package com.hj.dapp.business.budget.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hj.dapp.business.budget.model.Budget;
import com.hj.dapp.business.budget.model.BudgetExportVO;
import com.hj.dapp.business.budget.model.BudgetProject;
import com.hj.dapp.business.budget.service.BudgetService;
import com.hj.dapp.business.budget.vo.BudgetBindListVO;
import com.hj.dapp.business.budget.vo.BudgetPageVO;
import com.hj.dapp.business.budget.vo.BudgetProjectSave;
import com.hj.dapp.business.budget.vo.BudgetSaveVO;
import com.hj.dapp.business.craft.model.Craft;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.Principal;
import java.util.List;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/19
 */
@RestController
@RequestMapping("/budget")
public class BudgetController {

    @Autowired
    private BudgetService budgetService;

    @GetMapping("/exportLightAuxiliary/{customerId}/{engineerProjectId}")
    public void exportLightAuxiliary(HttpServletResponse response, @PathVariable("customerId") Long customerId, @PathVariable("engineerProjectId") Long engineerProjectId) {
        try {

            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition","attachment; filename*=UTF-8''" + URLEncoder.encode("轻辅预算表","UTF-8") + ".xlsx");

            response.flushBuffer();
            ServletOutputStream outputStream = response.getOutputStream();
            budgetService.exportLightAuxiliary(customerId, engineerProjectId,outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/getDiscountPrice/{customerId}/{engineerProjectId}")
    public BigDecimal getDiscountPrice(@PathVariable("customerId") Long customerId,@PathVariable("engineerProjectId") Long engineerProjectId) {
        return  budgetService.getDiscountPrice(customerId, engineerProjectId);
    }
    @PutMapping("save")
    public void save(@RequestBody BudgetSaveVO budgetSaveVO) {
        budgetService.save(budgetSaveVO);
    }

    @PutMapping("/saveBatch")
    public void saveBatch(@RequestBody List<BudgetSaveVO> budgetSaveVOList) {
        budgetService.saveBatch(budgetSaveVOList);
    }

    @PostMapping("/page")
    public IPage<BudgetPageVO> page(@RequestBody PageRequest<Budget> pageRequest, Principal principal) {
      return   budgetService.page(pageRequest, principal);
    }
    @PostMapping("/listBindCrafts")
    public List<Craft> listBindCrafts(@RequestBody BudgetBindListVO budget) {
        return  budgetService.listBindCrafts(budget);
    }
    @PostMapping("/saveProjects")
    public void saveProjects(@RequestBody BudgetProjectSave budgetProjectSave) {
        budgetService.saveProjects(budgetProjectSave);
    }
    @GetMapping("/getBudgetProjects/{budgetId}")
    public List<BudgetProject> getBudgetProjects(@PathVariable("budgetId") Long budgetId) {
        return budgetService.getBudgetProjects(budgetId);
    }
    @GetMapping("/list/{customerId}/{engineerProjectId}")
    public BudgetExportVO list(@PathVariable("customerId") Long customerId, @PathVariable("engineerProjectId") Long engineerProjectId) {
       return budgetService.list(customerId,engineerProjectId);
    }
    @GetMapping("/getProjects/{customerId}/{engineerProjectId}")
    public BudgetExportVO getProjects(@PathVariable("customerId") Long customerId, @PathVariable("engineerProjectId") Long engineerProjectId) {

        return budgetService.getProjects(customerId,engineerProjectId);
    }


}
